"""
Author        yongfa
Date          2022-07-11 16:14:19
LastEditTime  2023-09-05 03:07:34
LastEditors   yongfa
Description   功能描述
"""
import re
from decimal import Decimal
from app.extensions.exception import DataformatError


def split_int_list(params):
    """返回整型list"""
    return list(map(int, set(params.split(','))))


def split_decimal_list(params):
    """返回整型list"""
    result = params.split(',')
    return list(map(Decimal, result))


def split_str_list(params):
    """返回字符串list"""
    return list(set(params.split(',')))


def validate_phone(phone: str) -> str:
    """手机格式"""
    reg = "1[3|4|5|7|8][0-9]{9}"
    result = re.findall(reg, phone)
    if result and len(phone) == 11:
        return result[0]
    else:
        raise DataformatError(message='手机格式不正确')


def validate_password(password: str) -> str:
    """[密码格式]

    Args:
        password (str): [密码]

    Raises:
        DataformatError: [格式错误]

    Returns:
        str: [密码]
    """
    reg = r"^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$"
    result = re.findall(reg, password)
    if result and len(password) >= 8:
        return result[0]
    else:
        raise DataformatError(message='密码格式不正确，必须包含大小字母,数字,符号。且长度不小8！')


def validate_email(email: str) -> str:
    """[手机格式]

    Args:
        email (str): [description]

    Raises:
        DataformatError: [description]

    Returns:
        str: [description]
    """
    reg = r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)"
    result = re.findall(reg, email)
    if result:
        return result[0]
    else:
        raise DataformatError(message='邮箱格式不正确')


def validate_columns(class_name: object, args: dict) -> dict:
    """获取表字段，并除空值"""
    data = {}
    for attr, value in args.items():
        if attr in class_name.__table__.columns.keys() and value is not None:
            data[attr] = value

    return data
